import { BB_REPORT, dynamic_BB_REPORT_Column, dynamic_CW_KMB_Column, } from "./BaseReportTabPanel";
import { useMemo } from "react";
/**
 * 过滤表体数据, 根据所属报表类别进行数据过滤
 * @param rawTableData
 * @param searchCategory
 * @param searchTableName
 */
const filterByFromCategoryTableData = (rawTableData, searchCategory, searchTableName) => {
    if (rawTableData.length === 0) {
        return [];
    }
    if (searchTableName === BB_REPORT) {
        return rawTableData.filter((item) => {
            return item["所属报表"] === searchCategory;
        });
    }
    else {
        return rawTableData.filter((item) => {
            // 科目类别： 资产,负债 => 资产逻辑  其余都属于利润
            // 成本
            // 负债
            // 共同
            // 损益
            // 所有者权益
            // 资产
            if (searchCategory !== "利润") {
                return item["科目类型"].includes(searchCategory);
            }
            else {
                return (!item["科目类型"].includes("资产") &&
                    !item["科目类型"].includes("负债"));
            }
        });
    }
};
/**
 * 动态公式业务逻辑数据过滤
 * @param rawColumn
 * @param condition
 * @param searchCategory
 */
const filterColumnData = (rawColumn, condition, searchCategory) => {
    return rawColumn.filter((item) => {
        if (condition.common.includes(item.dataField)) {
            return true;
        }
        else if (condition[searchCategory] &&
            condition[searchCategory].includes(item.dataField)) {
            return true;
        }
        return false;
    });
};
/**
 * 前端过滤数据
 * @param dataSource
 * @param columnData
 * @param search
 * @param isDynamic
 */
const filterTableData = (dataSource, columnData, search, isDynamic) => {
    const { searchCategory, searchTableName } = search; // 根据所属报表进行前端级别数据过滤
    dataSource = filterByFromCategoryTableData(dataSource, searchCategory, searchTableName);
    // 如果是动态模式, 则按照动态条件业务过滤业务头
    if (isDynamic) {
        if (searchTableName === BB_REPORT) {
            columnData = filterColumnData(columnData, dynamic_BB_REPORT_Column, searchCategory);
        }
        else {
            columnData = filterColumnData(columnData, dynamic_CW_KMB_Column, searchCategory);
        }
    }
    return {
        dataSource,
        columns: columnData,
    };
};
/**
 * 报告基础数据取数源专用过滤
 * @param baseReportData
 * @param searchCategory
 * @param dynamic
 * @param searchTableName
 */
export const useBaseReportFilterData = (baseReportData, searchCategory, dynamic, searchTableName) => {
    return useMemo(() => {
        // 过滤数据服务目前只过滤, 动态类型和查询分类. 才进行触发.
        // 由于 searchTableName 的切换必然联动 searchCategory, 为了避免重复只监听 searchCategory 即可
        // 只有当数、表一致的情况下才进行触发
        // 空直接返回,跳过过滤
        if (baseReportData.dataSource.length === 0) {
            return baseReportData;
        }
        let checkData = null;
        if (searchTableName === BB_REPORT) {
            checkData = baseReportData.dataSource[0]["所属报表"];
        }
        else {
            checkData = baseReportData.dataSource[0]["科目类型"];
        }
        // 如果数、表不一致直接返回.
        if (!checkData) {
            return baseReportData;
        }
        return filterTableData(baseReportData.dataSource, baseReportData.columns, {
            searchCategory,
            searchTableName,
        }, dynamic.isDynamic && dynamic.dynamicModel);
    }, [baseReportData, searchCategory, dynamic, searchTableName]);
};
export default filterTableData;
